package com.efuture.collection;

import java.util.*;

/**
 * @ClassNAME CollectionsTest
 * @Description Collections的使用
 * @Author zhaoshanshan
 * @Date 2021/9/7 6:33 下午
 * @Version 1.0
 */
public class CollectionsTest {

    public static void main(String[] args) {
      List arrayList = new ArrayList<>();
      Collections.synchronizedList(arrayList);//变成线程安全的list
      arrayList.add("123");
      arrayList.add("457");
      arrayList.add("124");
      arrayList.add("145");
      Collections.sort(arrayList);//排序
      for (Object obj:arrayList){
          System.out.println(obj);
      }
       System.out.println("===============");
        List mySelfList = new ArrayList<>();
        mySelfList.add(new MySelf(10));
        mySelfList.add(new MySelf(5));
        mySelfList.add(new MySelf(19));
        mySelfList.add(new MySelf(33));
        Collections.sort(mySelfList);//排序
        for (Object mySelf:mySelfList){
            System.out.println(mySelf);
        }
        System.out.println("===============");
        Set<String> set = new HashSet<>();
        set.add("shanshan");
        set.add("tongshuai");
        set.add("zhaosi");
        set.add("yifan");
        List setList = new ArrayList(set);//set集合转换成List集合
        Collections.sort(setList);
        for (String str:set){
            System.out.println(str);
        }
    }
}
class MySelf implements Comparable<MySelf>{
   private int age;
    public MySelf(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(MySelf o) {
        return this.age-o.age;
    }

    @Override
    public String toString() {
        return "age="+this.age;
    }
}
